# Create your views here.
from django.shortcuts import render_to_response
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.http import HttpResponse
from leadership.home.models import *


def home(request):
	users = User.objects.order_by("urank")[0:10]
	return render_to_response("home/home.html" , {'users' : users})



def rank(request , uname="zinp" , list_style = "n"): 
	try:
		p = User.objects.get(name=uname)
	except User.DoesNotExist:
		return HttpResponse("sorry , we can't find this user")
	else:
		position = p.urank
	users = User.objects.filter(urank__gt=position).order_by("urank")[:20]
	return render_to_response("home/user.html" , {'users': users ,'p' : p , 'current' : "n"})

def get_name(request , uname):
	try:
		p = User.objects.get(name=uname)
	except User.DoesNotExist:
		return HttpResponse(0)
	else:
		position = p.urank
	page =int(( position + 9 ) / 10)
	return HttpResponse(page)

def get_page(request , cpage , uname):
	try:
		p = User.objects.get(name=uname)
	except User.DoesNotExist:
		p = 0
	cpage = int(cpage)
	start = int(10 * ( cpage - 1))
	end = int(10 * cpage)
	users = User.objects.order_by("urank")[start:end]
	#users = User.objects.filter(urank__range=(start,end))
	return render_to_response("home/table.html" , {"users" : users , 'p' : p  , 'page' : cpage})


def check(request , uname):
	try:
		p = User.objects.get(name=uname)
	except User.DoesNotExist:
		try:
			Queue.objects.get(name=uname)
		except Queue.DoesNotExist:
			cursor = Queue(name=uname)
			cursor.save()
		return HttpResponse("Sorry,this user does not exist in our database, please try later.")
	else:
		return HttpResponse("yes")

def about(request):
	return render_to_response("home/about.html")

def follower(request, uname):
	try:
		p = User.objects.get(name=uname)
	except User.DoesNotExist:
		return HttpResponse("sorry , we can't find this user")
	num = Leaders.objects.filter(user1=p.id).count()
	total = int((num + 19 ) / 20)
	page = int(request.GET.get('page' , 1))
	if page - 5 > 1:
		page_start = page - 5
	else:
		page_start = 1

	if page + 5 <= total:
		page_end = page + 5
	else:
		page_end = total

	#return HttpResponse(page_end)
	pagelist = range(page_start , page_end + 1)

	start = 20 * (page - 1)
	end = 20 * page
	users = Relation.objects.filter(user1=p.id)[start:end]
	return render_to_response("home/user.html" , {'users' : users , 'p' : p , "current" : "f" , "pagelist" : pagelist , "currentPage" : page})

def leader(request , uname):
	try:
		p = User.objects.get(name=uname)
	except User.DoesNotExist:
		return HttpResponse("sorry , we can't find this user")

	leaders = Leaders.objects.filter(user2=p.id)
	return render_to_response("home/user.html" , {'users' : leaders , 'p' : p , 'current' : "l"})

def get_relation(user ,ntype = "leader"):
	if ntype == "leader":
		expr = " WHERE user.id = relation.user1"
		leaders = Relation.objects.filter(user2=user.id).extra(
			select={
				'name' : 'SELECT user.name FROM user ' + expr,
				'score' : 'SELECT user.score FROM user ' + expr,
				'urank' : 'SELECT user.urank FROM user ' + expr,
			},
		)
	else:
		expr = " WHERE user.id = relation.user2"
		leaders = Relation.objects.filter(user1=user.id).extra(
			select={
				'name' : 'SELECT user.name FROM user ' + expr,
				'score' : 'SELECT user.score FROM user ' + expr,
				'urank' : 'SELECT user.urank FROM user ' + expr,
			},
		)


	for i in range(0 , len(leaders)):
		if ntype == "leader":
			userinfo = get_stat(leaders[i].user1)
		else:
			userinfo = get_stat(leaders[i].user2)

		leaders[i].followers = userinfo["followers"]
		leaders[i].leaders = userinfo["leaders"]
	
	return leaders


def get_stat(uid):
	followers = Relation.objects.filter(user1=uid).count()
	leaders = Relation.objects.filter(user2=uid).count()
	return {"followers" : followers , "leaders" : leaders}
